/*
* @Author: Josh
* @Date:   2019-02-01 14:37:01
* @Last Modified by:   Administrator
* @Last Modified time: 2019-05-03 16:50:33
*/
'use strict';

require('page/common/nav/index.js');
require('page/common/header/index.js');
require('./index.css');
var _mall         = require('util/mall.js'),
    _payment      = require('service/payment-service.js'),
    templateIndex = require('./index.string');

//page 逻辑部分
var page = {
    data : {
        orderNumber : _mall.getUrlParam('orderNumber')
    },
    init : function(){
        this.onLoad();
    },
    onLoad : function(){
        //加载商品详情数据
        this.loadPaymentInfo();
    },
    //加载支付页面
    loadPaymentInfo : function(){
        var paymentHtml = '',
            _this       = this,
            $pageWrap   = $('.page-wrap');

        $pageWrap.html('<div class="loading"></div>');

        _payment.getPaymentInfo(this.data.orderNumber, function(res){
            // 渲染html
            paymentHtml = _mall.renderHtml(templateIndex, res);
            $pageWrap.html(paymentHtml);

            _this.listenOrderStatus();

        }, function(errMsg){
            $pageWrap.html('<p class="err-tip">'+ errMsg +'</p>')
        });
    },
    //轮询监听订单状态
    listenOrderStatus : function(){
        var _this = this;
        this.paymentTimer = window.setInterval(function(){
            _payment.getPaymentStatus(_this.data.orderNumber, function(res){
                if(res == true){
                    window.location.href = 
                        './result.html?type=payment&orderNumber=' + _this.data.orderNumber;
                }
            });
        }, 5e3);
    }
};
    
$(function(){
    page.init(); 
});